use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};

#[derive(Debug, Clone)]
pub struct Metrics {
    pub data: Arc<RwLock<HashMap<String, i64>>>,
}

impl Metrics {
    pub fn new() -> Self {
        Metrics {
            data: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub fn inc(&self, key: &str) {
        let mut temp = self.data.write().unwrap();
        let counter = temp.entry(key.to_string()).or_insert(0);  // 根据key找 entry , 如果找不到， 设置value 为 0
        *counter += 1;
        // self.data
    }


    pub fn desc(&mut self, key: &str) {
        let mut temp = self.data.write().unwrap();
        let counter = temp.entry(key.to_string()).or_insert(0);
        *counter -= 1;
    }


    //  生成快照
    pub fn snapshot(&self) -> HashMap<String, i64> {
        let temp = self.data.read().unwrap();

        temp.clone()
    }
}
